package com.yowasa.xiuxian.engine.message.battle;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.battle.strategy.BattleStrategy;
import com.yowasa.xiuxian.engine.battle.strategy.BattleStrategyEngine;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.bot.message.Message;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.mapper.XiuxianUserBaseInfoMapper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.MessageFormat;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@CMD(name = "/设置", scope = CmdScopeEnum.PRIVATE)
public class ConfigMeHandler extends CmdMessageHandler {
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private XiuxianUserBaseInfoMapper xiuxianUserBaseInfoMapper;
    @Autowired
    private UserHelper userHelper;

    @Override
    public void execute(SendMessage message) {
        UserBO userBO = userHelper.checkOpUser(message);
        List<String> params = getAllParam(message);//获取名称
        if (StringUtils.isEmpty(params.get(0))) {
            messageHelper.replyMys(message, "可以在后面追加 查看 添加 删除 等二级指令进行后续操作");
            return;
        }
        Set<String> allStrategy = BattleStrategyEngine.strategyMap.keySet();
        if ("查看".equals(params.get(0))) {
            Set<String> battleStrategyList = userBO.getUserConfig().getBattleStrategyList();
            String msg = "可选战技有:\n" + String.join("\n", userBO.getUserConfig().getStudiedStrategies());
            msg += "\n已选战技:\n";
            if (CollectionUtils.isNotEmpty(battleStrategyList)) {
                msg += String.join("\n", battleStrategyList);
            } else {
                msg += "无";
            }
            messageHelper.replyMys(message, msg);
        }

        if ("添加".equals(params.get(0))) {
            if (params.size() < 2) {
                throw new HandlerException("请在添加后加上所选战技（空格隔开）");
            }
            String select = params.get(1);
            if (userBO.getUserConfig().getBattleStrategyList().contains(select)) {
                throw new HandlerException("你已经配置此战技 无需再配");
            }
            if (!allStrategy.contains(select)) {
                throw new HandlerException("未识别的战技类型,请先查看");
            }
            if (!userBO.getUserConfig().getStudiedStrategies().contains(select)) {
                throw new HandlerException("你未习得该战技 无法配置");
            }
            if (userBO.getUserConfig().getBattleStrategyList().size() > userBO.getUserConfig().getStrategyMax()) {
                throw new HandlerException(MessageFormat.format("你最多配置{0}个战技,超出了上限,请先/设置 删除 战技", userBO.getUserConfig().getStrategyMax()));
            }

            BattleStrategy battleStrategy = BattleStrategyEngine.strategyMap.get(select);
            if (!userBO.getUserConfig().getStudiedSpecialEffect().contains("太玄妙法") ) {
                battleStrategy.limit(userBO);
            }
            userBO.getUserConfig().getBattleStrategyList().add(select);
            userBO.buildBaseInfo();
            xiuxianUserBaseInfoMapper.updateById(userBO);
            messageHelper.replyMys(message, "设置成功");
        }

        if ("删除".equals(params.get(0))) {
            if (params.size() < 2) {
                throw new HandlerException("请在删除后加上所选战技（空格隔开）");
            }
            String select = params.get(1);
            if (!userBO.getUserConfig().getBattleStrategyList().contains(select)) {
                throw new HandlerException("你未配置过此战技 无需删除");
            }

            userBO.getUserConfig().getBattleStrategyList().remove(select);
            userBO.buildBaseInfo();
            xiuxianUserBaseInfoMapper.updateById(userBO);
            messageHelper.replyMys(message, "设置成功");
        }
    }

    public String desc(){
        return new StringBuffer().append("可追加 查看 添加 删除 进行后续操作\n")
                .append("/设置查看  查看当前已经配置的战技和可以配置的战技\n")
                .append("/设置添加  配置战斗中需要使用的战技\n")
                .append("/设置删除  将已经配置的战技移除，战斗中不使用此战技\n")
                .append("每个玩家最多同时配置四个战技\n")
                .toString();

    }
}
